﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EventMgrLib;
using Sunny.UI;

namespace SmartMotion.Core.Tools
{
    public partial class FrmScriptEdit : UIForm
    {
        private Script _script;
        private EventMgr _em;

        public FrmScriptEdit(Script script)
        {
            _script = script;
            InitializeComponent();
        }

        private void FrmScriptEdit_Load(object sender, EventArgs e)
        {
            _em=EventMgr.Ins;
            _em.GetEvent<DebugMessage>().Subscribe(ShowMessage, msg => msg.Contains(_script.ItemName));
            #region 加载内容

          
            RefreshDll();

            foreach (var dll in _script.DllList)
            {
                lstFileInUse.Items.Add(dll);
            }

            if (!string.IsNullOrEmpty(_script.Code))
            {
                rtbCode.Text = _script.Code;
            }

            #endregion
        }

        private void RefreshDll(bool loadAll = true)
        {
            var files = Directory.GetFiles(Environment.CurrentDirectory, "*.dll");
            lstFile.Items.Clear();
            foreach (var file in files)
            {
                try
                {
                    var types = ReflectAble.GetTypes(file, loadAll);
                    if (types.Count > 0)
                    {
                        lstFile.Items.Add(Path.GetFileName(file));
                    }
                }
                catch (Exception exception)
                {
                }
            }
        }

        private void lstFile_ItemDoubleClick(object sender, EventArgs e)
        {
            string dll = lstFile.SelectedItem.ToString();
            foreach (var item in lstFileInUse.Items)
            {
                if (item.ToString()==dll)
                {
                    return;
                }
            }

            lstFileInUse.Items.Add(dll);
            _script.DllList.Add(dll);
        }

        private void lstFileInUse_ItemDoubleClick(object sender, EventArgs e)
        {
            string dll = lstFileInUse.SelectedItem.ToString();
            lstFileInUse.Items.Remove(lstFileInUse.SelectedItem);

            _script.DllList.Remove(dll);
        }

        private void rtbCode_TextChanged(object sender, EventArgs e)
        {
            _script.Code = rtbCode.Text;
        }

        private void btnIni_Click(object sender, EventArgs e)
        {
            _script.Code = rtbCode.Text;
            _script.Initilize();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            _script.Run();
        }

        private void ShowMessage(string message)
        {
            txtOutput.AppendText(message+"\r");
        }

        private void FrmScriptEdit_FormClosed(object sender, FormClosedEventArgs e)
        {
            _em.GetEvent<DebugMessage>().Unsubscribe(ShowMessage);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshDll(ckbLoadAll.Checked);
        }
    }
}
